package org.jacp.demo.util;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;

import javax.swing.JComponent;

import org.jdesktop.swingx.graphics.ShadowRenderer;

public class ShadowedTumbnail extends JComponent {
    /**
     * 
     */
    private static final long serialVersionUID = 5938065455076432146L;
    private final BufferedImage original;
    private final BufferedImage shadow;
    // cached values for fast painting
    protected int distance_x = 0;
    protected int distance_y = 0;

    protected float angle = 30;
    protected int distance = 5;

    public ShadowedTumbnail(final BufferedImage original, final int shadowSize,
	    final float shadowOpacity, final Color shadowColor) {
	this.original = original;
	shadow = new ShadowRenderer(shadowSize, shadowOpacity, shadowColor)
		.createShadow(original);
	computeShadowPosition();
    }

    @Override
    protected void paintComponent(final Graphics g) {
	if (shadow != null) {
	    final int x = (getWidth() - shadow.getWidth()) / 2;
	    final int y = (getHeight() - shadow.getHeight()) / 2;
	    g.drawImage(shadow, x + distance_x, y + distance_y, null);
	}

	if (original != null) {
	    final int x = (getWidth() - original.getWidth()) / 2;
	    final int y = (getHeight() - original.getHeight()) / 2;
	    g.drawImage(original, x, y, null);
	}
    }

    private void computeShadowPosition() {
	final double angleRadians = Math.toRadians(angle);
	distance_x = (int) (Math.cos(angleRadians) * distance);
	distance_y = (int) (Math.sin(angleRadians) * distance);
    }

}
